clear all
set more off
cap log close

****************************************************************************
* This do-file extract data from the Read-only LAD files
*	- capital gains, income, dividends and rrsp related variables
* 
* Creates the following Stata files:
*	- capital_income_yyyy.dta
* 	- capital_income_allyears.dta (appended files)
* 
****************************************************************************

do "H:\Lavecchia_7086\to-transfer-jan-2022\RESTAT_Replication_Programs\0_Set_Directories.do"

cap log using "$dir_log\1_Extract_Capital_Income.log", replace

* Step 1: load the data from LAD read-only files

local year=1982
use lin__i tirc_i`year' invi_i`year' clkgli`year' rrspci`year' txi__i`year' xdiv_i`year' xtirci`year' divtxi`year' clkgxi`year' xtiici`year' using "G:\LAD\LAD_DAL_Allcycle_v3\data\data\Stata\lad`year'.dta"
sort lin__i
gen year=`year'
save "$dir_data\Capital income years\capital_income_`year'.dta", replace


forvalues year=1983/1985{
clear
use klpyci`year' lin__i tirc_i`year' invi_i`year' clkgli`year' rrspci`year' txi__i`year' xdiv_i`year' xtirci`year' divtxi`year' clkgxi`year' xtiici`year'  using "G:\LAD\LAD_DAL_Allcycle_v3\data\data\Stata\lad`year'.dta"
sort lin__i
gen year=`year'
save "$dir_data\Capital income years\capital_income_`year'.dta", replace
}

forvalues year=1986/2016{
clear
use ggex_i`year' klpyci`year'  lin__i tirc_i`year' invi_i`year' clkgli`year' rrspci`year' txi__i`year' xdiv_i`year' xtirci`year' divtxi`year' clkgxi`year' xtiici`year'  using "G:\LAD\LAD_DAL_Allcycle_v3\data\data\Stata\lad`year'.dta"
sort lin__i
gen year=`year'
save "$dir_data\Capital income years\capital_income_`year'.dta", replace
}




* Step 2: append into one file (rename the variables to drop the year portion of the name)

**************** 1982 -- 1999 ****************
clear
use "$dir_data\Capital income years\capital_income_1999.dta"
local i=1999
foreach var in ggex_i klpyci tirc_i invi_i clkgli rrspci txi__i xdiv_i xtirci divtxi clkgxi xtiici  {
rename `var'`i' `var'
}
*
forvalues i=1982/1998{
append using "$dir_data\Capital income years\capital_income_`i'.dta"
if `i'==1982{
gen ggex_i1982=.
gen klpyci1982=.
}
if `i'>=1983 & `i'<=1985{
gen ggex_i`i'=.
}

foreach var in ggex_i klpyci tirc_i invi_i clkgli rrspci txi__i xdiv_i xtirci divtxi clkgxi xtiici {
replace `var'= `var'`i' if year==`i'
drop `var'`i'
}
compress
}
*
save "$dir_data\capital_income_8299.dta", replace


***************** 2000 -- 2016 ****************
clear
use "$dir_data\Capital income years\capital_income_2000.dta"
local i=2000
foreach var in ggex_i klpyci tirc_i invi_i clkgli rrspci txi__i xdiv_i xtirci divtxi clkgxi xtiici {
rename `var'`i' `var'
}
*
forvalues i=2001/2016{
append using "$dir_data\Capital income years\capital_income_`i'.dta"
foreach var in ggex_i klpyci tirc_i invi_i clkgli rrspci txi__i xdiv_i xtirci divtxi clkgxi xtiici {
replace `var'= `var'`i' if year==`i'
drop `var'`i'
}
compress
}


save "$dir_data\capital_income_0016.dta", replace



log close

